package linter.linterjava.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageInfo;
import linter.linterjava.POJO.DO.Model;
import linter.linterjava.POJO.DO.ModelUserData;
import linter.linterjava.POJO.DTO.ModelDTO;
import linter.linterjava.POJO.DTO.ModelUserDataDTO;
import linter.linterjava.POJO.UtilDTO.FileDTO;
import linter.linterjava.POJO.UtilDTO.PageInfoDTO;
import linter.linterjava.mapper.ModelUserDataMapper;
import linter.linterjava.service.ModelService;
import linter.linterjava.mapper.ModelMapper;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

import static linter.linterjava.custom.ConstantPublicWay.getList;
import static linter.linterjava.custom.ConstantPublicWay.getPageInfo;
import static linter.linterjava.custom.CustomResultType.*;
import static linter.linterjava.utils.UploadFileUtil.uploadFileToQiniu;

/**
* @author 北煜
* @description 针对表【model(模型详情表)】的数据库操作Service实现
* @createDate 2024-07-07 12:15:30
*/
@Service
public class ModelServiceImpl extends ServiceImpl<ModelMapper, Model>
    implements ModelService{

    @Resource
    ModelMapper modelMapper;
    @Resource
    ModelUserDataMapper modelUserDataMapper;

    @Override
    public Long uploadData(ModelUserDataDTO modelUserDataDTO, Long uuid) {
        Date date = new Date();
        ModelUserData modelUserData = new ModelUserData();
        modelUserData.setUserId(uuid);
        modelUserData.setModelId(modelUserDataDTO.getModelId());
        modelUserData.setCreateTime(date);
        modelUserData.setUpdataTime(date);

        MultipartFile file = modelUserDataDTO.getFile();
        if (file != null && !file.isEmpty()) {
            String url = uploadFileToQiniu(file, uuid, modelUserDataDTO.getType());

            if (modelUserDataDTO.getType().equals("train")) {
                modelUserData.setTrainDataUrl(url);
                if (modelUserDataDTO.getTrainSelecetField() != null && !modelUserDataDTO.getTrainSelecetField().isEmpty()) {
                    modelUserData.setTrainSelecetField(modelUserDataDTO.getTrainSelecetField());
                }
            } else if (modelUserDataDTO.getType().equals("test")) {
                QueryWrapper<ModelUserData> queryWrapper = new QueryWrapper<>();
                queryWrapper.eq("user_id", uuid).eq("id", modelUserDataDTO.getId());
                ModelUserData existingData = modelUserDataMapper.selectOne(queryWrapper);
                if (existingData == null) {
                    return -1L;
                }
                modelUserData.setTestDataUrl(url);
                int flag = modelUserDataMapper.update(modelUserData, queryWrapper);
                return flag > 0 ? modelUserDataDTO.getId() : -1L;
            } else {
                return -1L;
            }

            int flag = modelUserDataMapper.insert(modelUserData);
            return flag > 0 ? modelUserData.getId() : -1L;
        }
        return -1L;
    }

    @Override
    public PageInfo<Model> selectAllModel(PageInfoDTO pageInfoDTO, Long uuid) {
        return getPageInfo(pageInfoDTO, new QueryWrapper<Model>().eq("is_delete", 0), modelMapper);
    }

    @Override
    public String uploadByUserSelDataPreprocess(ModelUserDataDTO modelUserDataDTO, Long uuid) {
        QueryWrapper<ModelUserData> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", modelUserDataDTO.getId()).eq("user_id", uuid);
        ModelUserData modelUserData = new ModelUserData();
        modelUserData.setPreprocess(modelUserDataDTO.getPreprocess());
        int flag = modelUserDataMapper.update(modelUserData, queryWrapper);
        return flag > 0 ? UPDATE_SUCCESS : UPDATE_ERROR;
    }

    @Override
    public List<Model> searchModel(ModelDTO modelDTO, Long uuid) {
        QueryWrapper<Model> queryWrapper = new QueryWrapper<>();
        if (modelDTO.getModelName() != null && !"".equals(modelDTO.getModelName())){
            queryWrapper.like("model_name", "%"+modelDTO.getModelName()+"%").eq("is_delete", 0);
        } else if (modelDTO.getModelEnglishName() != null && !"".equals(modelDTO.getModelEnglishName())){
            queryWrapper.like("model_english_name", "%"+modelDTO.getModelEnglishName()+"%").eq("is_delete", 0);
        } else if (modelDTO.getModelIntro() != null && !"".equals(modelDTO.getModelIntro())){
            queryWrapper.like("model_intro", "%"+modelDTO.getModelIntro()+"%").eq("is_delete", 0);
        }
        return modelMapper.selectList(queryWrapper);
    }
}




